"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalStrategy = void 0;
const passport_local_1 = require("passport-local");
const passport_1 = require("@nestjs/passport");
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const user_entity_1 = require("../entities/user.entity");
const tools_service_1 = require("../utils/tools.service");
let LocalStrategy = class LocalStrategy extends (0, passport_1.PassportStrategy)(passport_local_1.Strategy) {
    constructor() {
        super({
            usernameField: 'username',
            passwordField: 'password',
        });
    }
    async validateUser(userinfo) {
        const { username, password } = userinfo;
        const user = await this.usersRepository
            .createQueryBuilder('UsersEntity')
            .where('UsersEntity.username = :username', { username })
            .select(["UsersEntity"])
            .addSelect('UsersEntity.password')
            .getOne();
        if (!user)
            tools_service_1.ToolsService.fail('用户名不正确');
        console.log("🚀 ~ validateUser ~ user:", user);
        //使用bcryptjs验证密码
        if (password != user.password) {
            tools_service_1.ToolsService.fail('密码不正确');
        }
        return user;
    }
    //2.获取请求中的account和password参数进行验证。
    async validate(username, password) {
        const user = await this.validateUser({
            username,
            password,
        });
        console.log("🚀 ~ LocalStrategy ~ validate ~ user:", user);
        return user;
    }
};
exports.LocalStrategy = LocalStrategy;
__decorate([
    (0, typeorm_1.InjectRepository)(user_entity_1.UsersEntity)
], LocalStrategy.prototype, "usersRepository", void 0);
exports.LocalStrategy = LocalStrategy = __decorate([
    (0, common_1.Injectable)()
], LocalStrategy);
